﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using System.Web;

namespace WebApplication1
{
    public class SqlHelper
    {
        private string constr = "server=LAPTOP-8HS8J7NL;uid=sa;pwd=2287431003a;database=Student_db";
        private SqlConnection con = null;
        public SqlHelper()
        {
            con = new SqlConnection(constr);
        }
        public DataTable Get(string sql, SqlParameter[] pars)
        {
            try
            {
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                SqlCommand cmd = new SqlCommand(sql, con);
                if (pars != null && pars.Length != 0)
                {
                    cmd.Parameters.AddRange(pars);
                }
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adapter.Fill(ds);
                return ds.Tables[0];
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            finally
            {
                if (con != null)
                {
                    con.Close();
                }
            }
        }
        public bool Execute(string sql, SqlParameter[] pars)
        {
            try
            {
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                SqlCommand cmd = new SqlCommand(sql, con);
                if (pars != null && pars.Length != 0)
                {
                    cmd.Parameters.AddRange(pars);
                }
                return cmd.ExecuteNonQuery() > 0;
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            finally
            {
                if (con != null)
                {
                    con.Close();
                }
            }
        }
    }
}